﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;

namespace Web.SetSystem
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            //判断用户权限
            if (!((Account)HttpContext.Current.Session["Account"]).RoleCurrent.Equals('a')) Response.Redirect("Default.aspx");

            if (!IsPostBack) 
            { 
                GetTutors(); 
                GetAllClasses();
                if (DropDownList_classlist.SelectedItem.Text != "无") GetDefenseInfo();
            }                   
        }

        protected void GetTutors()
        {
            DataSet myDs = Teacher.GetAllTeacher();
            if (myDs.Tables[0].Rows.Count == 0) DropDownList_tutorlist.Items.Add("无");
            else
            {
                for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
                {
                    ListItem myListItem = new ListItem();
                    myListItem.Text = myDs.Tables[0].Rows[i]["tch_name"].ToString()+"("+myDs.Tables[0].Rows[i]["tch_id"].ToString()+")";
                    myListItem.Value = myDs.Tables[0].Rows[i]["tch_id"].ToString();
                    DropDownList_tutorlist.Items.Add(myListItem);
                }
            }
        }

        protected void GetAllClasses()
        {
            ArrayList myAL = Library.GetClassLib();
            if (myAL.Count == 0) DropDownList_classlist.Items.Add("无");
            else
                for (int j = 0; j < myAL.Count; j++)
                {
                    DropDownList_classlist.Items.Add(myAL[j].ToString());
                }
        }

        protected void GetDefenseInfo()
        {
            ClassInfo myCI = new ClassInfo(DropDownList_classlist.SelectedItem.Text);
            ArrayList myAL = myCI.GetStudentsList();
            DataTable myDT = new DataTable();
            myDT.Columns.Add("stu_id");
            myDT.Columns.Add("stu_name");
            myDT.Columns.Add("tutor_name");
            if (myAL.Count != 0)
            {
                for (int i=0; i < myAL.Count; i++)
                {
                    Student myStudent = new Student(myAL[i].ToString());
                    myDT.Rows.Add();                   
                    myDT.Rows[i]["stu_id"] = myStudent.StudentID;
                    myDT.Rows[i]["stu_name"] = myStudent.StudentName;
                    myDT.Rows[i]["tutor_name"] = Staff.GetName(myStudent.Papers.TeacherID);
                }
            }
            GridView_defenseinfo.DataSource = myDT;
            GridView_defenseinfo.DataBind();

        }

        protected void GridView_defenseinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_defenseinfo.PageIndex = e.NewPageIndex;
        }

        protected void ImageButton_changetutor_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList myAL = new ArrayList(15);
            for (int i = 0; i < GridView_defenseinfo.Rows.Count; i++)
            {
                if (((CheckBox)GridView_defenseinfo.Rows[i].Cells[0].FindControl("CheckBox_gridview")).Checked) myAL.Add(GridView_defenseinfo.Rows[i].Cells[1].Text); 
            }
            if (myAL.Count == 0) Label_Tutordefine_result.Text = "请选择学生";
            else
            {
                for (int j = 0; j < myAL.Count; j++)
                {
                    Student myStudent = new Student(myAL[j].ToString());
                    Teacher myTeacher = new Teacher(DropDownList_tutorlist.SelectedItem.Value);
                    string message = myTeacher.GuideStudents.Add(myStudent.StudentID);
                }
                GetDefenseInfo();
            }
        }

        protected void DropDownList_classlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDefenseInfo();            
        }

        protected void ImageButton_filter_Click(object sender, ImageClickEventArgs e)
        {
            if (TextBox_filter.Text == string.Empty)
            {
                DropDownList_classlist.Items.Clear();
                GetAllClasses();
                if (DropDownList_classlist.SelectedItem.Text != "无") GetDefenseInfo();
            }
            else
            {
                ArrayList myAL = new ArrayList();
                myAL = Library.GetClassLib(TextBox_filter.Text.ToString());
                DropDownList_classlist.Items.Clear();
                if (myAL.Count == 0) DropDownList_classlist.Items.Add("无");
                else
                {
                    for (int i = 0; i < myAL.Count; i++)
                        DropDownList_classlist.Items.Add(((ListItem)myAL[i]).Text);
                    GetDefenseInfo();
                }
            }
        }
    }
}
